/**
 * 通过时间日期字符串，获取日期对象
 */
const getDate = (dateStr) => {
	return new Date(Date.parse(dateStr.replace(/-/g, "/")));
}

/**
 * 将十位时间戳，转换成日期对象
 */
const getDateByTimestamp = (timestamp) => {
	return new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
}

/**
 * 将十位时间戳，转换成指定格式
 */
const getDateFormtByTimestamp = (timestamp, splitStr) => {
	if (!splitStr) {
		splitStr = "-";
	}
	let date = getDateByTimestamp(timestamp);
	let Year = date.getFullYear();
	let Month = (date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);

	return Year + splitStr + Month + splitStr + date.getDate();

}

/**
 * 将十位时间戳，转换成指定格式
 */
const getDateTimeFormtByTimestamp = (timestamp, splitStr) => {
	if (!splitStr) {
		splitStr = "-";
	}
	let date = getDateByTimestamp(timestamp);
	let Year = date.getFullYear();
	let Month = (date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	let day = date.getDate()
	if (day < 10) {
		day = '0' + day
	}
	let hours = date.getHours()
	if (hours < 10) {
		hours = '0' + hours
	}
	let minutes = date.getMinutes();
	if (minutes < 10) {
		minutes = '0' + minutes
	}
	let seconds = date.getSeconds();
	if (seconds < 10) {
		seconds = '0' + seconds
	}
	return Year + splitStr + Month + splitStr + day + ' ' + hours + ":" + minutes;

}



/**
 * 获取追加指定分钟时候过后的，日期对象
 */
const addMinute = (dateStr, addMinute) => {
	// 放映结束时间
	let date = this.getDate(dateStr);
	return new Date(date.setMinutes(date.getMinutes() + addMinute));

	// item.endTime = endTime.getHours() + ":" + endTime.getMinutes();
}

/**
 * 新增多少天
 */
const addDay = (dateStr, addDay) => {
	let date = getDate(dateStr);
	var ms = addDay * (1000 * 60 * 60 * 24)
	var newDate = new Date(date.getTime() + ms);
	return newDate;
}

/**
 * 计算两个日期相差多少天
 * @param dateStr1 比较早的时间
 * @param dateStr2 比较晚的时间
 */
const differDayByDateStr = (dateStr1, dateStr2) => {
	let date1 = getDate(dateStr1);
	let date2 = getDate(dateStr2);

	let differTime = date2.getTime() - date1.getTime();
	let difDay = parseInt(differTime / (1000 * 60 * 60 * 24));
	return difDay;
}

/**
 * 计算两个日期相差多少天
 * @param date1 比较早的时间
 * @param date2 比较晚的时间
 */
const differDayByDateObj = (date1, date2) => {
	let differTime = date2.getTime() - date1.getTime();
	let difDay = parseInt(differTime / (1000 * 60 * 60 * 24));
	return difDay;
}

/**
 * 计算两个日期相差多少分钟
 * @param date1 比较早的时间
 * @param date2 比较晚的时间
 */
const differMinuteByDateObj = (date1, date2) => {
	let differTime = date2.getTime() - date1.getTime();
	let diffMinute = parseInt(differTime / (1000 * 60));
	return diffMinute;
}

/**
 * 通过时间字符串，获取 今天、明天、后天名称
 */
const getChinaNameByDateStr = (dateStr) => {
	let currentDateStr = getDateFormtByTimestamp((new Date()).getTime() / 1000);
	let diffDay = differDayByDateStr(currentDateStr, dateStr);
	let showKey = '';
	switch (diffDay) {
		case 0:
			showKey = '今天';
			break;
		case 1:
			showKey = '明天';
			break;
		case 2:
			showKey = '后天';
			break;
		default:
			break;
	}
	return showKey;
}

/**
 * 将时间字符串 转换成时间戳
 */
const getTimeTampByStr = (dateStr) => {
	return parseInt(getDate(dateStr) / 1000);
}

/**
 * 获取当前时间，时间戳
 */
const getCurrentTimeTamp = () => {
	return parseInt(new Date().getTime() / 1000);
}

export default {
	getDate,
	getDateByTimestamp,
	getDateFormtByTimestamp,
	getDateTimeFormtByTimestamp,
	addMinute,
	addDay,
	differDayByDateStr,
	differDayByDateObj,
	differMinuteByDateObj,
	getChinaNameByDateStr,
	getTimeTampByStr,
	getCurrentTimeTamp,
}
